package com.yy.system.service;

import com.mes.entity.User;
import com.yy.base.common.service.BaseService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("loginService")
@SuppressWarnings("unchecked")
public class LoginService extends BaseService {
	/**
	 * 登录
	 */
	public User login(String userCode) {
		return (User)dao.findUnique("from User where userName=? and isactive='Y'", userCode);
	}
	
	public List getMenus(String userId) {
		return dao.find("from MenuInfo m where EXISTS (" +
				"select 1 from RoleMenus a, Role r where EXISTS(" +
				"select 1 from UserRole u where u.roleId = a.roleId and u.userId = ?" +
				") and a.menuId = m.id and a.roleId = r.id " +
				") and m.isActive = 'Y' " +
				"  and m.id <> '0' " +
				"ORDER BY parentId, seq", userId);
	}
	
	/**
	 * 获取用户
	 * @param user
	 * @param orgId
	 * @return
	 */
	public Map getLoginInfos(User user,String orgId){
		String sql = " select ao.name from ad_org ao where ao.ad_org_id = ?";
		return dao.queryUnique(sql, orgId);
	}

	/**
	 * 用户信息与微信绑定
	 * @param userCode
	 * @param openId
	 * @return
	 */
	public int updateUser(String userCode,String openId){
		System.out.println(openId);
		String sql = " update AD_USER set weixin = ? where AD_USER_ID = ?";
		return dao.executeSql(sql,openId,userCode);
	}
}